package yxy.game.pm2.active.command.challenge;

import yxy.cherry.data.bean.actchallenge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.handle.Act_TimeChallengeHandle;
import yxy.game.pm2.active.module.ActiveModule_限时挑战;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/** 限时挑战 挑战 */
@MessageType(proto = Command.ActChanllenge, classtype = ActiveMessage.ActChanllengeRequest.class)
public class CMD_Active_限时挑战_Chanllenge extends Command<ActiveMessage.ActChanllengeRequest> {

	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActChanllengeRequest request) {
		String playerCode = messageSource.getTag();
		int id = request.getId();

		ActiveMessage.ActChanllengeResponse.Builder response = ActiveMessage.ActChanllengeResponse.newBuilder();

		Act_TimeChallengeHandle handle = new Act_TimeChallengeHandle();

		//条件判断
		ActiveModule_限时挑战 am = ActiveModule_限时挑战.get(playerCode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getGetrewardids().contains(id) || am.getVictoryids().contains(id)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		actchallenge execl = ActDataCenter.getData(id, actchallenge.class, playerCode);
		if (execl == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}

		try {
			response.setBattle(handle.battleResponse(playerCode, id));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}

	}

	private void send(MessageSource messageSource, ActiveMessage.ActChanllengeResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}